package com.ciwong.mobilelib.utils.animtoast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.ciwong.mobilelib.R;


/**
 * date :2018/11/29
 * author: lzh
 * description: 加强版toast 可以实现toast栓需执行
 * version:
 */

class ToastAnim extends BaseToastAnim {
    public static final long LENGTH_3000 = 3000;
    public static final long LENGTH_2500 = 2500;
    public static final long LENGTH_2000 = 2000;
    protected Context mContext;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowParams;
    private View toastView;
    private Handler mHandler;
    private CharSequence mToastContent = "";
    private long duration = 0;
    protected TextView mTextView;
    private IRemoveViewListener iRemoveViewListener;

    public ToastAnim(Context context) {
        Context ctx = context.getApplicationContext();
        if (ctx == null) {
            ctx = context;
        }
        this.mContext = ctx;
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        init();
    }


    private void init() {
        mWindowParams = new WindowManager.LayoutParams();
        mWindowParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
        mWindowParams.alpha = 1.0f;
        mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        mWindowParams.format = PixelFormat.TRANSLUCENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        } else {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        }
        mWindowParams.setTitle("ToastHelper");
        mWindowParams.packageName = mContext.getPackageName();
    }

    /**
     * @param context
     * @param content
     * @param duration
     * @return
     */
    public static ToastAnim makeText(Context context, String content,
                                     long duration) {
        ToastAnim helper = new ToastAnim(context);
        helper.setDuration(duration);
        helper.setContent(content);
        return helper;
    }

    /**
     * @param context
     * @param strId
     * @param duration
     * @return
     */
    public static ToastAnim makeText(Context context, int strId, int duration) {
        ToastAnim helper = new ToastAnim(context);
        helper.setDuration(duration);
        helper.setContent(context.getString(strId));
        return helper;
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private View getDefaultToastView() {
        ViewGroup inflate = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.toast_anim_integral, null);
        inflate.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        inflate.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
        mTextView = inflate.findViewById(R.id.tv_integral_toast);
        if (mToastContent != null)
            mTextView.setText(mToastContent);
        mTextView.setVisibility(View.INVISIBLE);
        return inflate;
    }


    public void show() {
        if (toastView == null) {
            toastView = getDefaultToastView();
        }
        show(toastView, duration);
    }

    private void show(View toastView, long duration) {
        mWindowManager.addView(toastView, mWindowParams);
        addAnim(toastView, mWindowManager, mWindowParams, duration);
    }


    private void removeView(View toastView) {
        if (toastView != null && toastView.getParent() != null) {
            mWindowManager.removeViewImmediate(toastView);
        }
    }

    protected ToastAnim setContent(CharSequence content) {
        this.mToastContent = content;
        return this;
    }

    protected ToastAnim setDuration(long duration) {
        if (duration <= 0) return this;
        this.duration = duration;
        return this;
    }

    /**
     * custom view
     *
     * @param view
     */
    protected ToastAnim setView(View view) {
        this.toastView = view;
        return this;
    }

    protected void addAnim(final View toastView, final WindowManager mWindowManager, final WindowManager.LayoutParams windowParams, long duration) {
        ToastAnimContal.addAnim(mContext, toastView, getmTextView(), mWindowManager, windowParams, duration, new IFinishAnimToast() {
            @Override
            public void animFiish(View view) {
                removeView(view);
                if (iRemoveViewListener != null)
                    iRemoveViewListener.viewRemoved();
            }
        });
    }

    public View getToastView() {
        return toastView;
    }

    public long getDuration() {
        return duration;
    }

    public TextView getmTextView() {
        return mTextView;
    }

    public interface IRemoveViewListener {
        void viewRemoved();
    }

    public void setOnRemoveViewListener(IRemoveViewListener IRemoveViewListener) {
        iRemoveViewListener = IRemoveViewListener;
    }
}
